package com.luxsoft.sw3.ui.consultas;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.jdesktop.swingx.JXTable;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.uif.builder.ToolBarBuilder;
import com.luxsoft.em2.replication.validation.Validacion;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;


public class ValidadorPanel extends FilteredBrowserPanel<Validacion>{
	
	private String tabla;
	private String entidad;
	private String replicador;

	public ValidadorPanel() {
		super(Validacion.class);
		
	}
	
	protected void init(){
		addProperty(
				"tabla"
				,"entidad"
				,"concepto"
				,"sucursalId"
				,"fecha"
				,"registrosEnSucursal"
				,"registrosEnCentral"
				,"control_1_sucursal"
				,"control_1_central"
				,"control_1_desc"
				,"control_2_sucursal"
				,"control_2_central"
				,"control_2_desc"
				,"control_3_sucursal"
				,"control_3_central"
				,"control_3_desc"
				,"actualizacion"
				);
		addLabels("Tabla"
				,"Entidad"
				,"Concepto"
				,"Sucursal"
				,"Fecha"
				,"Reg (Suc)"
				,"Reg (Cen)"
				,"Ctrl 1(Suc)"
				,"Ctrl 1(Cen)"
				,"Ctrl 1 Desc"
				,"Ctrl 2(Suc)"
				,"Ctrl 2(Cen)"
				,"Ctrl 2 Desc"
				,"Ctrl 3(Suc)"
				,"Ctrl 3(Cen)"
				,"Ctrl 3 Desc"
				,"Actualizacion"
				);
		manejarPeriodo();
		installTextComponentMatcherEditor("Tabla", "tabla");
		installTextComponentMatcherEditor("Entidad", "entidad");
		installTextComponentMatcherEditor("Concepto", "concepto");
		installTextComponentMatcherEditor("Sucursal", "sucursalId");

	}
	
	
	
	@Override
	protected JComponent buildContent() {
		final JPanel panel=new JPanel(new BorderLayout());
		panel.add(buildToolbar(),BorderLayout.NORTH);
		panel.add(buildGridPanel(buildGrid()),BorderLayout.CENTER);
		panel.add(getFilterPanel(),BorderLayout.SOUTH);
		afterGridCreated();		
		
		return panel;
	}
	
	protected DefaultFormBuilder getFilterPanelBuilder(){
		if(filterPanelBuilder==null){
			FormLayout layout=new FormLayout(
					"p,1dlu,p,2dlu," +
					"p,1dlu,p,2dlu," +
					"p,1dlu,p,2dlu," +
					"p,1dlu,p,2dlu"
					,"");
			DefaultFormBuilder builder=new DefaultFormBuilder(layout);
			builder.getPanel().setOpaque(false);
			filterPanelBuilder=builder;
		}
		return filterPanelBuilder;
	}
	
	protected JToolBar buildToolbar(){
		ToolBarBuilder builder=new ToolBarBuilder();
		for(Action a:getActions()){
			builder.add(a);
		}
		return builder.getToolBar();
	}

	@Override
	protected void adjustMainGrid(JXTable grid) {
		for(int i=14;i<=16;i++)
			grid.getColumnExt(14).setVisible(false);
	}

	@Override
	public Action[] getActions() {
		if(actions==null)
			actions=new Action[]{
				getLoadAction()				
				};
		return actions;
	}

	@Override
	protected List<Validacion> findData() {
		String hql="from Validacion v where v.fecha between ? and ? ";
		Object[] params=new Object[]{periodo.getFechaInicial(),periodo.getFechaFinal()};
		if(StringUtils.isNotBlank(getTabla())){
			hql+=" and v.tabla=?";
			params=ArrayUtils.add(params, getTabla());
		}if(StringUtils.isNotBlank(getEntidad())){			
			hql+=" and v.entidad=?";
			params=ArrayUtils.add(params, getEntidad());
		}
		return ServiceLocator2.getHibernateTemplate()
		.find(hql,params);
		
	}

	public String getTabla() {
		return tabla;
	}

	public void setTabla(String tabla) {
		this.tabla = tabla;
	}

	public String getEntidad() {
		return entidad;
	}

	public void setEntidad(String entidad) {
		this.entidad = entidad;
	}

	public String getReplicador() {
		return replicador;
	}

	public void setReplicador(String replicador) {
		this.replicador = replicador;
	}
	
	

}
